package com.bilicraft.plugin.kexue;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;

import com.bilicraft.plugin.kexue.commands.*;

public class Shell {
	
	/** this shell's user*/
	public ShellUser shellUser;
	/** command dispatch map*/
	public Map<String, BiliCmd> cmdTable = new HashMap<String, BiliCmd>();
	
	public Shell(ShellUser shelluser){
		shellUser = shelluser;
		try {
			shelluser.curdir = (new File(shelluser.curdir).getCanonicalPath());
		} catch (IOException e) {
			e.printStackTrace();
		}
		cmdTable.put("cat", new BiliCmdCat());
		cmdTable.put("cd", new BiliCmdCd());
		cmdTable.put("get", new BiliCmdGet());
		cmdTable.put("udplg", new BiliCmdUdplg());
		cmdTable.put("update", new BiliCmdUpdate());
		cmdTable.put("ls", new BiliCmdLs());
		cmdTable.put("mkdir", new BiliCmdMkdir());
		cmdTable.put("pwd", new BiliCmdPwd());
		cmdTable.put("reset", new BiliCmdReset());
		cmdTable.put("rm", new BiliCmdRm());
		cmdTable.put("cleandata", new BiliCmdCleandata());
		cmdTable.put("unzip", new BiliCmdUnzip());
		cmdTable.put("json", new BiliCmdJson());
		cmdTable.put("rename", new BiliCmdRename());
	}
	/** dispatch commands*/
	public boolean exec(CommandSender sender, Command cmd, String label, String[] args){
		BiliCmd bilicmd = cmdTable.get(cmd.getName().toLowerCase().replaceAll("_", ""));
		if(bilicmd != null){
			return bilicmd.exec(sender, cmd, label, args, shellUser);
		}
		return false;
	}
}
